package com.api.util;

import com.api.pojo.Case;

import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**用户关键类
 * 优化用例数据，接口ID和用例ID关联
 */
public class CaseUtil {
    public static List<Case> cases = new ArrayList<Case>();
    static {
        ExcelUtil.loadDatas(PropertiesUtil.getCaseFilePath(), "用例", Case.class);

    }
    public static void main(String[] args){

    for(Case cs:cases){
        System.out.println(cs);
    }
    }


    /**获取对应接口的用例数据
     * @param apiId  接口编号
     * @param cellNames 要取得列名
     * @return
     */
    public static Object[][] datas(String apiId, String[] cellNames) {
        //根据apiId筛选出对应的接口对象信息
        List<Case> satisfied = new ArrayList<Case>();
        for(Case cs :cases){
            if(cs.getApiId().equals(apiId)){
                satisfied.add(cs);

            }

        }
        Object [] [] datas = new Object[satisfied.size()][cellNames.length];
        Class clazz = Case.class;
        for(int i=0;i<satisfied.size();i++){
            //
            Case cs= satisfied.get(i);
            for(int j=0;j<cellNames.length;j++){
                String cellName = cellNames[j];
                String methodName="get"+ cellName;
                Method method = null;
                try {
                    method = clazz.getMethod(methodName);
                    String value = (String) method.invoke(cs);
                    datas[i][j]=value;
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

        }
        return datas;

    }
}
